<?php
class  Front_Model_DbTable_Common  extends  Zend_Db_Table_Abstract
{
    public function test(){
    	return 'ok';
    }
    public function insertData($table_name, $data){
    	try{
    		$this->_db->insert ($table_name,$data);
    	}catch(Exception $ex){
    		echo '<br/>Can not insert data to '.$table_name.'<br/>Error: '.$ex->getMessage();
    		exit;	
    	}
    }
    
    public function generateActivekey($password) {
    	try {
    		do {
    			$key = md5 ( time () );
    			$key .= md5 ( $password );
    			$sql = 'SELECT activekey FROM employee where activekey = "' . $key . '"';
    			$db = $this->_db;
    			$db->setFetchMode ( Zend_Db::FETCH_OBJ );
    			$activekey = $db->fetchAll ( $sql );
    		} while ( ! empty ( $activekey ) );
    		return $key;
    	} catch ( Exception $ex ) {
    		Echo ("<br /> generate Active Key Failed, error: " . $ex->getMessage ());
    		
    		exit ();
    	}
    }
    
    public function getFid($table_name){
    	try {
	    	$sql = 'Select id from '.$table_name.' order by id desc limit 1';
	    	$db = $this->_db;
	    	$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
	    	$fid = $db->fetchAll ( $sql );
	    	if(empty($fid))
	    		$fid[0]['id'] = 0;
    		$fid = (int)($fid[0]['id']) + 1;
	    	return $fid;
    	} catch ( Exception $ex ) {
    		Echo ("<br /> Can not get final id, error: " . $ex->getMessage ());
    		exit ();
    	}
    }
    
    public function getAll($table_name, $where=""){
    	try {
    		$this->_db->setFetchMode(Zend_Db::FETCH_ASSOC);
    		$sql = (empty($where))? 'SELECT * FROM `'.$table_name.'`' : 'SELECT * FROM `'.$table_name.'` where '.$where;
    		return $this->_db->fetchAll ( $sql );
    	} catch ( Exception $ex ) {
   			echo ("<br /> Can not get all information, <br/>Error: " . $ex->getMessage ());
			echo "<br/>sql la: ".$sql;
    		exit ();
    	}
    }
    
    public function getColumn($table_name, $column_name = array(), $where=""){
    	try {
	    	$sql = 'Select ';
	    	$max = count($column_name) -1;
	    	for($i = 0; $i < $max ; ++$i)
	    		$sql .= $one_column[$i].', ';
	    	if(!empty($column_name[$max])) $sql .= $column_name[$max];
	    	$sql .=  ' from '.$table_name.' where '.$where;
	    	
	    	$db = $this->_db;
	    	$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
	    	$result = $db->fetchAll ( $sql );
	    	return $result;
    	} catch ( Exception $ex ) {
    		Echo ("<br /> Can not get column, error: " . $ex->getMessage ());
    		exit ();
    	}
    }
    
    
    
    public function getAlbum($id )
    {
    	$id = (int)$id ;
    	
    	$row = $this->fetchRow( 'id = '  . $id );
    	
    	if  (!$row) {
    		throw new Exception("Could not find row $id ");
    	}
    	return $row->toArray();
    }
    
    public function  addAlbum($artist, $title )
    {
    	$data  = array(
    			'artist' =>  $artist,
    			'title'  =>  $title ,
    	);
    	$this->insert($data);
    }
    
    public function  updateAlbum($id , $artist, $title )
    {
    	$data  = array(
    			'artist' =>  $artist,
    			'title'  =>  $title ,
    	);
    	$this->update($data, 'id = '. (int)$id );
    }
    
    public function  deleteAlbum($id )
    {
    	$this->delete('id =' . (int)$id );
    }
    
}
